﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using OpenMms.Domain.Models;

namespace OpenMms.MVC.Areas.User.Controllers
{
    [Area("User")]
    public class UserInforMationsController : Controller
    {
        private readonly OpenMmsDBContext _context;

        public UserInforMationsController(OpenMmsDBContext context)
        {
            _context = context;
        }

        // GET: User/UserInforMations
        public async Task<IActionResult> Index()
        {
            var dbset = _context.Set<UserInforMations>().ToList();
            return View(await _context.UserInforMations.ToListAsync());
        }

        // GET: User/UserInforMations/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var userInforMations = await _context.UserInforMations
                .FirstOrDefaultAsync(m => m.UserId == id);
            if (userInforMations == null)
            {
                return NotFound();
            }

            return View(userInforMations);
        }

        // GET: User/UserInforMations/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: User/UserInforMations/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("UserId,ServiceCentreCode,RefereeCode,UserLevel,UserCode,UserPassWord,UserSecondPassWord,RegistionTime,UserName,IdNumber,Phone,SecurityQuestion,SecretAnswer,BankName,BankCode,CardHolder,IsCenterService,UserStatus,LevelName,Area,ContactCode,Bonus,LeftCount,RightCount,LeftAchie,RightAchie,RefereeCount,VirtualMoney,RealMoney,ActivationTime,RefereeId,ContactId,Investment,ManagerLevel")] UserInforMations userInforMations)
        {
            if (ModelState.IsValid)
            {
                _context.Add(userInforMations);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(userInforMations);
        }

        // GET: User/UserInforMations/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var userInforMations = await _context.UserInforMations.FindAsync(id);
            if (userInforMations == null)
            {
                return NotFound();
            }
            return View(userInforMations);
        }

        // POST: User/UserInforMations/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("UserId,ServiceCentreCode,RefereeCode,UserLevel,UserCode,UserPassWord,UserSecondPassWord,RegistionTime,UserName,IdNumber,Phone,SecurityQuestion,SecretAnswer,BankName,BankCode,CardHolder,IsCenterService,UserStatus,LevelName,Area,ContactCode,Bonus,LeftCount,RightCount,LeftAchie,RightAchie,RefereeCount,VirtualMoney,RealMoney,ActivationTime,RefereeId,ContactId,Investment,ManagerLevel")] UserInforMations userInforMations)
        {
            if (id != userInforMations.UserId)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(userInforMations);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserInforMationsExists(userInforMations.UserId))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(userInforMations);
        }

        // GET: User/UserInforMations/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var userInforMations = await _context.UserInforMations
                .FirstOrDefaultAsync(m => m.UserId == id);
            if (userInforMations == null)
            {
                return NotFound();
            }

            return View(userInforMations);
        }

        // POST: User/UserInforMations/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var userInforMations = await _context.UserInforMations.FindAsync(id);
            _context.UserInforMations.Remove(userInforMations);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool UserInforMationsExists(int id) => _context.UserInforMations.Any(e => e.UserId == id);
    }
}
